package com.lxw.oa.service.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.lxw.oa.entity.*;
import com.lxw.oa.mapper.ChartsConfigMapper;
import com.lxw.oa.service.ChartsConfigService;
import com.lxw.oa.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("chartsConfigService")
@Transactional
public class ChartsConfigServiceImpl implements ChartsConfigService {

	@Autowired
	private ChartsConfigMapper chartsConfigMapper;

	public List<ChartsCfgFunction> getCfgFunctionByFuncLink(String func_link) {
		List<ChartsCfgFunction> list=chartsConfigMapper.getCfgFunctionByFuncLink(func_link);
		return list;
	}


	public List<ChartsCfgBarData> getCfgBarDataByFuncId(String func_id){
		List<ChartsCfgBarData> list=chartsConfigMapper.getCfgBarDataByFuncId(func_id);
		return list;
	}

	public List<ChartsCfgPieData> getCfgPieDataByFuncId(String func_id) {
		List<ChartsCfgPieData> list=chartsConfigMapper.getCfgPieDataByFuncId(func_id);
		return list;
	}

	public List<ChartsCfgRadarData> getCfgRadarDataByFuncId(String func_id) {
		List<ChartsCfgRadarData> list=chartsConfigMapper.getCfgRadarDataByFuncId(func_id);
		return list;
	}


	
	public List getChartsDataBySQL(HttpServletRequest request, String data_id, String sql) {
		List paramsList = new ArrayList();
		List valuesList = new ArrayList();
		List<ChartsCfgCondition> condList = getCfgCondByDataId(data_id);
		String cond_field = "";
		String default_value = "";
		String _val = "";
		for(int i=0,j=condList.size(); i<j; i++) {
			ChartsCfgCondition cond = condList.get(i);
			cond_field = cond.getCond_field();
			default_value = cond.getDefault_value();
			if("null".equalsIgnoreCase(default_value) || StringUtil.isEmpty(default_value)) {
				default_value = ""; 
			}
			if(!StringUtil.isEmpty(cond_field)) {
				_val = request.getParameter(cond_field);
				if(StringUtil.isEmpty(_val)) {
					_val = default_value;
				}
			} else {
				_val = default_value;
			}
			if("null".equalsIgnoreCase(_val) || StringUtil.isEmpty(_val)) { _val = ""; }
			paramsList.add("@"+cond_field+"@");
			valuesList.add(_val);
		}
		String rsql = parserSql(sql, paramsList, valuesList);
		List colList = chartsConfigMapper.getList(rsql);
		
		System.out.println("[getChartsDataBySQL-paramsList]:"+paramsList);
		System.out.println("[getChartsDataBySQL-valuesList]:"+valuesList);
		System.out.println("[getChartsDataBySQL-rsql]:"+rsql);
		
		return colList;
	}

	public String parserSql(String sql, List paramsList, List valuesList) {

		String newsql = sql;
		for(int i=0; i<paramsList.size(); i++) {
			newsql = newsql.replaceAll(String.valueOf(paramsList.get(i)), String.valueOf(valuesList.get(i)));
		}

		return newsql;
	}



	
	public List<ChartsCfgCondition> getCfgCondByDataId(String data_id) {
		List<ChartsCfgCondition> list = chartsConfigMapper.getCfgCondByDataId(data_id);
		return list;
	}

}
